
//2563.统计公平数对的数目
class Solution {
public:
    long long countFairPairs(vector<int>& nums, int lower, int upper) {
        //排序+二分
        int n=nums.size();
        sort(nums.begin(),nums.end());

        long long ret=0;
        for(int i=0;i<n;i++)
        {
            //[lower-nums[i],upper-num[i]]

            int start=lower_bound(nums.begin(),nums.end(),lower-nums[i])-nums.begin();
            int end=upper_bound(nums.begin(),nums.end(),upper-nums[i])-nums.begin();
            if(start<=i) start=i+1;

            if(end>start)
            ret+=end-start;
        }
        return ret;
    }
};